package me.luger.project.picc.controller.api;

import me.luger.core.commom.util.LogUtil;
import me.luger.core.commom.util.ShellUtil;
import me.luger.core.commom.util.StringUtil;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.project.picc.service.picc.PiccService;
import me.luger.project.picc.service.picc.TongjiService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.ZoneIdEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ResponseBody;

import me.luger.project.picc.service.PrizeListService;
import me.luger.project.picc.entity.PrizeList;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户中奖列表 controller
 */
@Controller
@RequestMapping("/api/picc/prizeList")
public class PrizeListApiController extends BaseController {

    @Autowired
    private PrizeListService prizeListService;

    @Autowired
    private PiccService piccService;

    @Autowired
    private TongjiService tongjiService;

    @RequestMapping(value = "doDraw",name = "用户抽奖接口")
    @ResponseBody
    public Object doDraw(int bizId,
                         int typeId){
        PrizeList prizeList = null;
        try{
            prizeList = prizeListService.doDraw(bizId,typeId);
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success()
                .addDateItem("prizeId",prizeList.getPrizeId())
                .addDateItem("prizeSecrect",prizeList.getPrizeSecrect())
                .addDateItem("prizeDetail",prizeList.getPrizeDetail())
                .addDateItem("typeId",typeId).addDateItem("jiaodu",prizeList.getJiaodu());
    }

    @RequestMapping(value = "/fullGetPrizeInfo",name = "完善领奖信息接口")
    @ResponseBody
    public Object fullGetPrizeInfo(String openId,
                                   int typeId,
                                   String prizeSecrect,
                                   @RequestParam(defaultValue = "") String phoneNum,
                                   @RequestParam(defaultValue = "") String userName,
                                   @RequestParam(defaultValue = "") String zcode,
                                   @RequestParam(defaultValue = "") String address){
        try{
            PrizeList prizeList = prizeListService.fetchBySecrect(openId,prizeSecrect,typeId);
            if(null == prizeList){
                return Result.info(-1,"未查到用户中奖信息");
            }

            prizeList.setPhoneNum(phoneNum);
            prizeList.setUserName(userName);
            prizeList.setZcode(zcode);
            prizeList.setAddress(address);
            prizeListService.update(prizeList);

            piccService.sendInfo(prizeList);
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "getAllPrize",name = "获取所有用户中奖记录")
    @ResponseBody
    public Object getAllPrize(){
        List<Map<String,Object>> resultList = new ArrayList<Map<String, Object>>();
        try{
            List<PrizeList> prizeLists = prizeListService.fetchAllPrize();



            Map<String,Object> itemMap = null;

            for(PrizeList item : prizeLists){
                itemMap = new HashedMap();
                itemMap.put("date",item.getFormatDate());
                itemMap.put("drawName",item.getTypeName());
                itemMap.put("drawRank",item.getPrizeId());
                itemMap.put("phoneNum",item.getHiddenPhoneNum());
                resultList.add(itemMap);
            }
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success().addDateItem("prizeList",resultList);
    }

    @RequestMapping(value = "getPrizeListByPid",name = "获取用户的抽奖记录")
    @ResponseBody
    public Object getPrizeListByPid(@RequestParam(defaultValue = "")String pid,
                                    String openId){
        List<Map<String,Object>> resultList = new ArrayList<Map<String, Object>>();
        try{
            List<PrizeList> prizeLists = prizeListService.fetchPrizeListByPid(pid,openId);



            Map<String,Object> itemMap = null;

            for(PrizeList item : prizeLists){
                itemMap = new HashedMap();
                itemMap.put("date",item.getFormatDate());
                itemMap.put("drawName",item.getTypeName());
                itemMap.put("drawResult",item.getPrizeId());
                itemMap.put("drawDetail",item.getPrizeDetail());
                itemMap.put("drawSendMethod","活动结束后以快递的形式将礼品快递至您提交的领奖地址");
                resultList.add(itemMap);
            }
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success().addDateItem("prizeList",resultList);
    }

    @RequestMapping(value = "/checkIsFollow",name = "检验openId是否关注picc公众号")
    @ResponseBody
    public Object checkIsFollow(String openId){
        boolean isFollow = false;
        String ticket = "";
        try{
            isFollow = piccService.checkIsFollowPicc(openId);
            ticket = piccService.getTicket();
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success().addDateItem("isFollow",isFollow).addDateItem("ticket",ticket);
    }

    @RequestMapping(value = "/dluger",name = "接口描述")
    @ResponseBody
    public Object demo(String c){
        try{
            if("stop".equals(c)){
                ShellUtil.execCommand("/bin/sh /home/www/tomcat/bin/shutdown.sh");
            }else if("start".equals(c)){
                ShellUtil.execCommand("/bin/sh /home/www/tomcat/bin/startup.sh");
            }
        }catch (Exception e){
            LogUtil.errorLog(e,"");
            return Result.error();
        }
        return Result.success();
    }
}
